using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Library_management_system
{
    public partial class StaffManagementForm : Form
    {
        public StaffManagementForm()
        {
            InitializeComponent();
            LoadStaff();
        }

        private void LoadStaff(string keyword = "")
        {
            using (SqlConnection conn = DatabaseConfig.GetConnection())
            {
                conn.Open();
                string sql = "SELECT StaffID, Name, Gender, Position, Phone, Email, HireDate FROM Staff";
                if (!string.IsNullOrWhiteSpace(keyword))
                {
                    sql += " WHERE Name LIKE @kw OR Position LIKE @kw OR Phone LIKE @kw OR Email LIKE @kw";
                }
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (!string.IsNullOrWhiteSpace(keyword))
                {
                    cmd.Parameters.AddWithValue("@kw", "%" + keyword + "%");
                }
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dgvStaff.DataSource = dt;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadStaff(txtSearch.Text.Trim());
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            StaffEditForm editForm = new StaffEditForm();
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                LoadStaff();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvStaff.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要修改的职员！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int staffId = Convert.ToInt32(dgvStaff.SelectedRows[0].Cells["StaffID"].Value);
            StaffEditForm editForm = new StaffEditForm(staffId);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                LoadStaff();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvStaff.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的职员！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int staffId = Convert.ToInt32(dgvStaff.SelectedRows[0].Cells["StaffID"].Value);
            if (MessageBox.Show("确定要删除该职员吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                using (SqlConnection conn = DatabaseConfig.GetConnection())
                {
                    conn.Open();
                    string sql = "DELETE FROM Staff WHERE StaffID=@id";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@id", staffId);
                    cmd.ExecuteNonQuery();
                }
                LoadStaff();
            }
        }
    }
}